package com.xydtech.ecard.config.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardBusiReportPublic;
import com.xydtech.ecard.config.mapper.EcardBusiAndConfigMapper;
import com.xydtech.ecard.config.mapper.EcardBusiReportPublicMapper;
import com.xydtech.ecard.config.service.IEcardBusiReportPublicService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 电子名片业务填报对公类信息Service业务层处理
 *
 * @author xydtech
 * @date 2024-09-29
 */
@Service
public class EcardBusiReportPublicServiceImpl implements IEcardBusiReportPublicService
{
    @Autowired
    private EcardBusiReportPublicMapper ecardBusiReportPublicMapper;
    @Autowired
    private EcardBusiAndConfigMapper ecardBusiAndConfigMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片业务填报对公类信息
     *
     * @param id 电子名片业务填报对公类信息主键
     * @return 电子名片业务填报对公类信息
     */
    @Override
    public EcardBusiReportPublic selectEcardBusiReportPublicById(Long id, String salt)
    {
        EcardBusiReportPublic busiReportPublic = ecardBusiReportPublicMapper.selectEcardBusiReportPublicById(id);
        if (busiReportPublic != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportPublic.getPubPhone())) {
                    busiReportPublic.setPubPhone(AESEncryptionUtils.decrypt(busiReportPublic.getPubPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return busiReportPublic;
    }

    /**
     * 查询电子名片业务填报对公类信息列表
     *
     * @param ecardBusiReportPublic 电子名片业务填报对公类信息
     * @return 电子名片业务填报对公类信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EcardBusiReportPublic> selectEcardBusiReportPublicList(EcardBusiReportPublic ecardBusiReportPublic, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPublic.getPubPhone())) {
                ecardBusiReportPublic.setPubPhone(AESEncryptionUtils.encrypt(ecardBusiReportPublic.getPubPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<EcardBusiReportPublic> busiReportPublicList = ecardBusiReportPublicMapper.selectEcardBusiReportPublicList(ecardBusiReportPublic);
        if (busiReportPublicList.size() > 0) {
            for (EcardBusiReportPublic busiReportPublic : busiReportPublicList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportPublic.getPubPhone())) {
                        busiReportPublic.setPubPhone(AESEncryptionUtils.decrypt(busiReportPublic.getPubPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReportPublicList;
    }

    /**
     * 新增电子名片业务填报对公类信息
     *
     * @param ecardBusiReportPublic 电子名片业务填报对公类信息
     * @return 结果
     */
    @Override
    public int insertEcardBusiReportPublic(EcardBusiReportPublic ecardBusiReportPublic, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPublic.getPubPhone())) {
                ecardBusiReportPublic.setPubPhone(AESEncryptionUtils.encrypt(ecardBusiReportPublic.getPubPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportPublicMapper.insertEcardBusiReportPublic(ecardBusiReportPublic);
    }

    /**
     * 修改电子名片业务填报对公类信息
     *
     * @param ecardBusiReportPublic 电子名片业务填报对公类信息
     * @return 结果
     */
    @Override
    public int updateEcardBusiReportPublic(EcardBusiReportPublic ecardBusiReportPublic, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPublic.getPubPhone())) {
                ecardBusiReportPublic.setPubPhone(AESEncryptionUtils.encrypt(ecardBusiReportPublic.getPubPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportPublicMapper.updateEcardBusiReportPublic(ecardBusiReportPublic);
    }

    /**
     * 批量删除电子名片业务填报对公类信息
     *
     * @param ids 需要删除的电子名片业务填报对公类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportPublicByIds(String ids)
    {
        //循环删除电子名片业务填报信息和业务类型关联表
        for (String busiId : ids.split(",")) {
            ecardBusiAndConfigMapper.deleteEcardBusiAndConfigByConfigIdAndBusiType(busiId, "3");
        }
        return ecardBusiReportPublicMapper.deleteEcardBusiReportPublicByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片业务填报对公类信息信息
     *
     * @param id 电子名片业务填报对公类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportPublicById(Long id)
    {
        return ecardBusiReportPublicMapper.deleteEcardBusiReportPublicById(id);
    }

    /**
     * 导出电子名片业务填报对公类信息列表
     * @param ecardBusiReportPublic
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LinkedHashMap<String, Object>> selectEcardBusiReportPublicListByExport(EcardBusiReportPublic ecardBusiReportPublic, String salt) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardBusiReportPublic.setDictDeptId(sysDept.getDeptId());
        List<LinkedHashMap<String, Object>> ecardBusiReportPublics = ecardBusiReportPublicMapper.selectEcardBusiReportPublicListByExport(ecardBusiReportPublic);
        List<LinkedHashMap<String, Object>> ecardBusiReportPublicMaps = new ArrayList<>();
        for (LinkedHashMap<String, Object> busiReportPublicMap : ecardBusiReportPublics) {
            LinkedHashMap<String, Object> maps = new LinkedHashMap<>();
            maps.put("企业名称", busiReportPublicMap.get("pub_ent_name"));
            maps.put("统一社会信用代码", busiReportPublicMap.get("pub_credit_code"));
            maps.put("业务名称", busiReportPublicMap.get("busi_name"));
            maps.put("积分", busiReportPublicMap.get("integral_nums"));
            maps.put("联系人", busiReportPublicMap.get("pub_contacts"));
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportPublicMap.get("pub_phone"))) {
                    maps.put("联系方式", AESEncryptionUtils.decrypt(String.valueOf(busiReportPublicMap.get("pub_phone")), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            String busiCode = String.valueOf(busiReportPublicMap.get("busi_code"));
            if ("busi_pub_International".equals(busiCode)){
                maps.put("结汇金额（美元）", busiReportPublicMap.get("pub_settlement_amount"));
            } else if ("busi_pub_deposit".equals(busiCode)){
                maps.put("存款类型", busiReportPublicMap.get("pub_deposit_type"));
                maps.put("存款金额", busiReportPublicMap.get("pub_deposit_amount"));
                maps.put("存款期限", busiReportPublicMap.get("pub_deposit_term"));
            } else if ("busi_pub_loan".equals(busiCode)){
                maps.put("贷款类型", busiReportPublicMap.get("pub_loan_type"));
                maps.put("贷款金额", busiReportPublicMap.get("pub_loan_amount"));
                maps.put("贷款期限", busiReportPublicMap.get("pub_loan_term"));
                maps.put("贷款利率", busiReportPublicMap.get("pub_loan_rate"));
            }
            maps.put("营销日期", busiReportPublicMap.get("pub_marketing_date"));
            maps.put("备注说明", busiReportPublicMap.get("remark"));
            maps.put("创建时间", busiReportPublicMap.get("creation_time"));
            maps.put("营销经理", busiReportPublicMap.get("user_name"));
            maps.put("员工号", busiReportPublicMap.get("user_code"));
            maps.put("归属机构", busiReportPublicMap.get("dept_name"));
            ecardBusiReportPublicMaps.add(maps);
        }
        return ecardBusiReportPublicMaps;
    }
}
